
#include <BSlider>
#include "member_BSlider.h"

using namespace BWE;

member_BSlider::member_BSlider(BSlider* slider)
{
	boss = slider;
	value = 0;
	step = 1;
	minimum = 0;
	maximum = 100;
	orientation = Orientation_Horizontal;
	handleState = State_Normal;
}
member_BSlider::~member_BSlider()
{

}

int member_BSlider::valueOfPoint(const BPoint& pos) const
{
	int value = 0;
	if (orientation & Orientation_Horizontal)
	{
		if (orientation & Orientation_Reverse)
			value = (double)(rect.right() - pos.x()) / rect.width() * (maximum - minimum);
		else
			value = (double)(pos.x() - rect.left()) / rect.width() * (maximum - minimum);
	}
	if (orientation & Orientation_Vertical)
	{
		if (orientation & Orientation_Reverse)
			value = (double)(pos.y() - rect.top()) / rect.height() * (maximum - minimum);
		else
			value = (double)(rect.bottom() - pos.y()) / rect.height() * (maximum - minimum);
	}
	value = bLimit(value, minimum, maximum);
	return value;
}
void member_BSlider::freshRects()
{
	int w = rect.width();
	int h = rect.height();

	float range = maximum - minimum;
	if (orientation & Orientation_Horizontal)
	{
		if (orientation & Orientation_Reverse)
		{
			int x = rect.right() - (w * value / range) - h / 2;
			handleRect.set(x, rect.y(), h, h);
		}
		else
		{
			int x = rect.left() + (w * value / range) - h / 2;
			handleRect.set(x, rect.y(), h, h);
		}
	}
	if (orientation & Orientation_Vertical)
	{
		if (orientation & Orientation_Reverse)
		{
			int y = rect.top() + (h * value / range) - w / 2;
			handleRect.set(rect.x(), y, w, w);
		}
		else
		{
			int y = rect.bottom() - (h * value / range) - w / 2;
			handleRect.set(rect.x(), y, w, w);
		}
	}
}
